package com.tongxin.van.frameworkcanary.dailog;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;

import com.blankj.utilcode.util.ScreenUtils;
import com.tongxin.van.frameworkcanary.R;
import com.tongxin.van.frameworkcanary.databinding.DialogConfirmBinding;

/**
 *
 * author: VanyLiu
 * email: 13212674795@163.com
 * description:确认对话框
 *
**/

public class ConfirmDialog extends Dialog {

    DialogConfirmBinding mBinding;

    public ConfirmDialog(@NonNull Context context) {
        super(context);

        // 隐藏dialog标题头
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //设置窗口背景透明，这样才能显示自己的对话框背景
        assert getWindow() != null;
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        //设置布局
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.dialog_confirm, null, false);
        setContentView(mBinding.getRoot());

        //对话框宽度
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = (int) (ScreenUtils.getScreenWidth() * 0.7);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(lp);

        //取消按钮
        mBinding.btnCancel.setOnClickListener(v -> dismiss());
    }

    /**
     * 消息内容
     */
    public ConfirmDialog setMessage(String text) {
        mBinding.tvMsg.setText(text);
        return this;
    }

    /**
     * 取消按钮
     */
    public ConfirmDialog setCancelButton(String text) {
        mBinding.btnCancel.setText(text);
        return this;
    }

    /**
     * 确认按钮
     */
    public ConfirmDialog setConfirmButton(String text) {
        mBinding.btnConfirm.setText(text);
        return this;
    }

    /**
     * 是否可以取消
     */
    public ConfirmDialog setDialogCancelable(boolean b) {
        setCancelable(b);
        return this;
    }

    /**
     * 确认按钮监听事件
     */
    public ConfirmDialog setOnConfirmListener(OnConfirmListener listener) {
        mBinding.btnConfirm.setOnClickListener(v -> {
            listener.onConfirm(this);
        });
        return this;
    }

    /**
     * 确认按钮监听接口
     */
    public interface OnConfirmListener {
        void onConfirm(ConfirmDialog dialog);
    }


}
